<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 后台结算页商品ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   allen.qiang
 */

quanli_require('module/base/goodsHandler.class.php');
quanli_require('module/base/customerHandler.class.php');
quanli_require('module/base/promotionHandler.class.php');
quanli_require('module/base/systemHandler.class.php');

class ajax_checkoutGoods {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询商品详情
                    $this->searchGoodsDetail();
                    break;

                case 2: //查询商品列表
                    $this->searchGoodsList();
                    break;

                case 5: //根据商品查询订单促销活动
                    $this->searchBillPromotion();
                    break;

            }

        }

    }


    public function searchGoodsList() {

        if (!isset($_REQUEST['pcustID']) || !is_numeric($_REQUEST['pcustID']))
            die( json_encode( array( 'code' => 400, 'message' => '参数错误' ) ) );

        global $G_PGLOB;

        if (!isset($_REQUEST['page']) || !is_numeric($_REQUEST['page']))
            $_REQUEST['page'] = 1;

        //实例化商品类
        $goodsHandler = new goodsHandler( $G_PGLOB->DBCA );

        //查询商品列表
        $_REQUEST['sortname'] = 'postID';
        $_REQUEST['sortorder'] = 'DESC';
        $_REQUEST['rp'] = 5;
        $goods_list_array = $goodsHandler->searchGoodsList( $_REQUEST );
        if ($goods_list_array) {

            foreach($goods_list_array as &$goods_list) {

                //查询三级商品
                $arg_array = array(
                    'twoPostID' => $goods_list['postID'],
                    'sortname' => 'postID',
                    'sortorder' => 'DESC'
                );
                $goods_detail_array = $goodsHandler->searchGoodsSale( $arg_array );
                if (!$goods_detail_array)
                    continue;

                foreach($goods_detail_array as &$g) {

                    if (!isset($result_goods_array[$goods_list['postID']])) {
                        $result_goods_array[$goods_list['postID']] = array(
                            'postID' => $goods_list['postID'],
                            'goodsName' => $goods_list['goodsName'],
                            'spec' => $goods_list['spec'],
                            'list' => null
                        );
                    }


                    $result_goods_array[$goods_list['postID']]['list'][] = array(
                        'postID' => $g['postID'],
                        'goodsName' => $g['goodsName'],
                        'spec' => $g['spec']
                    );

                }

            }

            //实例化公共类
            $commHandler = new commHandler();

            //生成分页程序
            $paging = $commHandler->shopPage( $_REQUEST['page'], $_REQUEST['count'], $_REQUEST['rp'], 'javascript: bill.goods.general.search(%d);', $G_PGLOB->smarty, 'comm/page.tpl');

            echo json_encode( array( 'code' => 200, 'message' => 'ok', 'data' => $result_goods_array, 'paging' => $paging ) );

        } else {

            echo json_encode( array( 'code' => 404, 'message' => '商品信息没有找到' ) );

        }




    }


    public function searchBillPromotion() {

        if (!isset($_REQUEST['goods_array']) || empty($_REQUEST['goods_array']))
            die( json_encode( array( 'code' => 400, 'message' => '参数错误' ) ) );

        global $G_PGLOB;

        //实例化促销类
        $promotionHandler = new promotionHandler( $G_PGLOB->DBCA );

        //查询订单促销活动
        $promotion_array = $promotionHandler->searchBillPromotion( $_REQUEST['goods_array'] );


        //实例化系统类
        $sysHandler = new systemHandler( $G_PGLOB->DBCA );

        //计算运费
        $freight = $sysHandler->checkoutCalculateFreight($_REQUEST['send_area_id'], $_REQUEST['weight']);

        echo json_encode( array( 'code' => 200, 'message' => 'ok', 'data' => $promotion_array, 'freight' => $freight ) );

    }


    /**
     * 查询商品详情
     */
    private function searchGoodsDetail() {

        if (!isset($_REQUEST['pcustID']) || !is_numeric($_REQUEST['pcustID']))
            die( json_encode( array( 'code' => 400, 'message' => '参数错误' ) ) );

        global $G_PGLOB;

        //实例化商品类
        $goodsHandler = new goodsHandler( $G_PGLOB->DBCA );

        $goods_detail_array = null;

        //查询商品详情
        if (isset($_REQUEST['product_code'])) {

            $arg_array = array(
                'product_code' => $_REQUEST['product_code']
            );
            $goods_detail_array[] = $goodsHandler->searchGoodsSale( $arg_array );

        } else if (isset($_REQUEST['postStr'])) {

            $id_array = explode(',', $_REQUEST['postStr']);
            unset($id_array[0]);

            foreach($id_array as &$postID) {
                $arg_array = array(
                    'postID' => $postID
                );
                $goods_detail_array[] = $goodsHandler->searchGoodsSale( $arg_array );
            }

        } else {
            die( json_encode( array( 'code' => 400, 'message' => '参数错误' ) ) );
        }



        if ($goods_detail_array) {

            //实例化会员类
            $customerHandler = new customerHandler( $G_PGLOB->DBCA );

            //获得会员等级信息
            $cust_rank = $customerHandler->getCustRand( $_REQUEST['pcustID'] );

            foreach($goods_detail_array as &$goods_array) {

                //计算商品实售价格及查询促销活动
                $goodsHandler->shopCalculationGoodsPrice($cust_rank, $goods_array);


                $result_goods_array[] = array(
                    'goodsID' => $goods_array['goodsID'],
                    'postID' => $goods_array['postID'],
                    'goodsName' => $goods_array['goodsName'],
                    'twoPostID' => $goods_array['twoPostID'],
                    'price' => $goods_array['price'],
                    'salePrice' => $goods_array['salePrice'],
                    'marketPrice' => $goods_array['marketPrice'],
                    'addPoints' => 0,
                    'salePoints' => $goods_array['salePoints'],
                    'goods_amount' => 1,
                    'imgLink' => $goods_array['img_link'],
                    'link' => $goods_array['link'],
                    'product_code' => $goods_array['product_code'],
                    'spec' => $goods_array['spec'],
                    'goodsWeight' => $goods_array['goodsWeight'],
                    'promotion' => $goods_array['promotion']
                );

            }

            echo json_encode( array( 'code' => 200, 'message' => 'ok', 'data' => $result_goods_array ) );
        } else {
            echo json_encode( array( 'code' => 404, 'message' => '商品信息没有找到' ) );
        }

    }

}